package cn.topcodes.tcsf.admin.service.impl;

import cn.topcodes.tcsf.admin.commons.generic.LogType;
import cn.topcodes.tcsf.admin.domain.entity.SystemProperty;
import cn.topcodes.tcsf.admin.domain.form.BaseSettings;
import cn.topcodes.tcsf.admin.service.SystemSettingsService;
import cn.topcodes.tcsf.admin.service.log.SysOperateLogService;
import cn.topcodes.tcsf.admin.service.SystemPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * @author Unicorn
 * @date 2017/4/19
 */
@Service
@Transactional(readOnly = true)
public class SystemSystemSettingsServiceImpl implements SystemSettingsService {

    @Autowired
    private SystemPropertyService propertyService;

    @Autowired
    private SysOperateLogService logService;

    @Override
    @Transactional(readOnly = false)
    public void setBaseSettings(Long userId, BaseSettings baseSettings) {
        Assert.notNull(baseSettings);
        logService.log(userId, LogType.UPDATE, "修改系统基本设置 " + baseSettings);
        SystemProperty propSiteName = this.propertyService.findSysProp(SITE_NAME);
        if(propSiteName == null) {
            this.propertyService.createSysProp(SITE_NAME,baseSettings.getSiteName());
        }else {
            this.propertyService.modifySysProp(SITE_NAME,baseSettings.getSiteName());
        }
        SystemProperty propCopyright = this.propertyService.findSysProp(COPYRIGHT);
        if(propCopyright == null) {
            this.propertyService.createSysProp(COPYRIGHT,baseSettings.getCopyright());
        }else {
            this.propertyService.modifySysProp(COPYRIGHT,baseSettings.getCopyright());
        }
    }

    @Override
    public BaseSettings getBaseSettings() {
        String siteName = this.propertyService.findSysProperty(SITE_NAME,String.class,DEFAULT_SITE_NAME);
        String copyRight = this.propertyService.findSysProperty(COPYRIGHT,String.class,DEFAULT_COPYRIGHT);
        BaseSettings baseSettings = new BaseSettings();
        baseSettings.setSiteName(siteName);
        baseSettings.setCopyright(copyRight);
        return baseSettings;
    }
}
